/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import org.jdesktop.layout.GroupLayout;

import wydruk.Drukarnia;

public class OWydruk extends JFrame {

    public OWydruk(java.util.List lk) {
        this.lk = lk;
        st = new String[3];
        sdf = new SimpleDateFormat("dd-MM-yyyy");
        String s = sdf.format(new Date());
        st = s.split("-");
        initComponents();
        txtDay.setText(st[0]);
        txtMonth.setText(st[1]);
        txtYear.setText(st[2]);
        jRadioButton1.setSelected(true);
        jRadioButton2.setSelected(false);
        jRadioButton3.setSelected(false);
        txtKasa.setEnabled(false);
    }

    private void initComponents() {
        jPanel1 = new JPanel();
        jRadioButton1 = new JRadioButton();
        jRadioButton2 = new JRadioButton();
        jRadioButton3 = new JRadioButton();
        jPanel2 = new JPanel();
        jLabel1 = new JLabel();
        txtDay = new JTextField();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        txtMonth = new JTextField();
        txtYear = new JTextField();
        jLabel4 = new JLabel();
        txtKasa = new JTextField();
        jLabel5 = new JLabel();
        jButton1 = new JButton();
        jButton2 = new JButton();
        setDefaultCloseOperation(2);
        setTitle("Wyb\363r wydruku");
        jPanel1.setBorder(BorderFactory.createEtchedBorder());
        jRadioButton1.setText("Polecenie Zap\u0142aty");
        jRadioButton1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton1.setMargin(new Insets(0, 0, 0, 0));
        jRadioButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
            final OWydruk this$0;
            

            {
                this$0 = OWydruk.this;
            //super();
            }
        });
        jRadioButton2.setText("Upomnienie");
        jRadioButton2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton2.setMargin(new Insets(0, 0, 0, 0));
        jRadioButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
            final OWydruk this$0;
            

            {
                this$0 = OWydruk.this;
            //super();
            }
        });
        jRadioButton3.setText("Informacje o Kliencie");
        jRadioButton3.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jRadioButton3.setMargin(new Insets(0, 0, 0, 0));
        jRadioButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
            final OWydruk this$0;
            

            {
                this$0 = OWydruk.this;
            //super();
            }
        });
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jPanel1Layout.createParallelGroup(1).add(jRadioButton1).add(jRadioButton2).add(jRadioButton3)).addContainerGap(-1, 32767)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jRadioButton1).addPreferredGap(0).add(jRadioButton2).addPreferredGap(0).add(jRadioButton3).addContainerGap(-1, 32767)));
        jLabel1.setText("Termin zap\u0142aty");
        txtDay.setText("12");
        jLabel2.setText("-");
        jLabel3.setText("-");
        txtMonth.setText("12");
        txtYear.setText("2004");
        jLabel4.setText("Koszt upomnienia");
        txtKasa.setHorizontalAlignment(11);
        txtKasa.setText("0.0");
        jLabel5.setText("[z\u0142]");
        GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(1).add(jPanel2Layout.createSequentialGroup().addContainerGap().add(jPanel2Layout.createParallelGroup(1).add(jPanel2Layout.createSequentialGroup().add(jLabel1).addPreferredGap(0).add(txtDay, -2, -1, -2).add(4, 4, 4).add(jLabel2).add(4, 4, 4).add(txtMonth, -2, -1, -2).add(3, 3, 3).add(jLabel3)).add(jPanel2Layout.createSequentialGroup().add(jLabel4).addPreferredGap(0).add(txtKasa, -2, 48, -2))).add(jPanel2Layout.createParallelGroup(1).add(jPanel2Layout.createSequentialGroup().addPreferredGap(0).add(jLabel5)).add(jPanel2Layout.createSequentialGroup().add(3, 3, 3).add(txtYear, -2, -1, -2))).addContainerGap(70, 32767)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(1).add(jPanel2Layout.createSequentialGroup().addContainerGap().add(jPanel2Layout.createParallelGroup(3).add(jLabel1).add(txtDay, -2, -1, -2).add(jLabel2).add(txtMonth, -2, -1, -2).add(jLabel3).add(txtYear, -2, -1, -2)).add(22, 22, 22).add(jPanel2Layout.createParallelGroup(3).add(jLabel4).add(jLabel5).add(txtKasa, -2, -1, -2)).addContainerGap(-1, 32767)));
        jButton1.setText("OK");
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OWydruk this$0;
            

            {
                this$0 = OWydruk.this;
            //super();
            }
        });
        jButton2.setText("anuluj");
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OWydruk this$0;
            

            {
                this$0 = OWydruk.this;
            //super();
            }
        });
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().add(layout.createParallelGroup(2, false).add(1, jPanel2, -1, -1, 32767).add(1, layout.createSequentialGroup().addContainerGap().add(jPanel1, -2, -1, -2).add(34, 34, 34).add(layout.createParallelGroup(1, false).add(jButton1, -1, -1, 32767).add(jButton2)))).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(layout.createSequentialGroup().addContainerGap().add(jPanel2, -2, -1, -2).addPreferredGap(0).add(layout.createParallelGroup(2).add(jPanel1, -2, -1, -2).add(layout.createSequentialGroup().add(jButton1).addPreferredGap(0).add(jButton2))).addContainerGap(-1, 32767)));
        pack();
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        try {

            String s = (new StringBuilder()).append(txtDay.getText()).append("-").append(txtMonth.getText()).append("-").append(txtYear.getText()).toString();
            Date d = sdf.parse(s);
            Float k = Float.valueOf(Float.parseFloat(txtKasa.getText().replace(",", ".")));
            if (jRadioButton1.isSelected()) {


                try {
                    Drukarnia.getInstance().drukujKlient(lk, "xml"+File.separator+"wezwanie_do_zaplaty.xml", d);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                
                dispose();
            } else if (jRadioButton2.isSelected()) {
                try {
                    //usun klientow z zerowymi naleznosciami z listy do drukowania
                    List<Klient> lk_wydruk = new ArrayList<Klient>();
                    int size=lk.size(); 
                    for(int i=0;i < size; i++){
                        if(((Klient)lk.get(i)).getSumaZostalo()> 0.0){
                            lk_wydruk.add((Klient) lk.get(i));
                        }
                    }
                    Drukarnia.getInstance().drukujKlientUpomnienie(lk_wydruk, "xml"+File.separator+"ponaglenie.xml", d, k.floatValue());
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                dispose();


           
            } else if (jRadioButton3.isSelected()) {
                try {
                    Drukarnia.getInstance().drukujKlient(lk, "xml"+File.separator+"info_klient.xml", d);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                
                dispose();
            }
        } catch (ParseException exc) {
            JOptionPane.showMessageDialog(this, "B\u0142ad podczas wprowadzania daty - (sprawdz czy pola daty zawieraja odpowiednie warto\u015B\u0107i)");
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "B\u0142ad podczas wprowadzania danych - (sprawdz czy pola liczbowe nie zawieraj\u0105 liter \noraz u\u017Cywaj kropki do oddzielenia cz\u0119\u015Bci u\u0142amkowej)");
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        dispose();
    }

    private void jRadioButton3ActionPerformed(ActionEvent evt) {
        jRadioButton1.setSelected(false);
        jRadioButton2.setSelected(false);
        jRadioButton3.setSelected(true);
        txtKasa.setEnabled(false);
        txtDay.setEnabled(false);
        txtMonth.setEnabled(false);
        txtYear.setEnabled(false);
    }

    private void jRadioButton2ActionPerformed(ActionEvent evt) {
        jRadioButton1.setSelected(false);
        jRadioButton2.setSelected(true);
        jRadioButton3.setSelected(false);
        txtKasa.setEnabled(true);
        txtDay.setEnabled(true);
        txtMonth.setEnabled(true);
        txtYear.setEnabled(true);
    }

    private void jRadioButton1ActionPerformed(ActionEvent evt) {
        jRadioButton1.setSelected(true);
        jRadioButton2.setSelected(false);
        jRadioButton3.setSelected(false);
        txtKasa.setEnabled(false);
        txtDay.setEnabled(true);
        txtMonth.setEnabled(true);
        txtYear.setEnabled(true);
    }
    private SimpleDateFormat sdf;
    private String st[];
    private java.util.List lk;
    private JButton jButton1;
    private JButton jButton2;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JRadioButton jRadioButton1;
    private JRadioButton jRadioButton2;
    private JRadioButton jRadioButton3;
    private JTextField txtDay;
    private JTextField txtKasa;
    private JTextField txtMonth;
    private JTextField txtYear;
}
